package com.example.beikeyigou.login_reg;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.beikeyigou.R;

import java.util.ArrayList;
import java.util.List;

public class LocalImgActivity extends AppCompatActivity {
    GridView gv;
    List<String> data;
    private LocalImgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_img);

        gv = findViewById(R.id.localImg_gv);
        // 数据源
        data = new ArrayList<>();
        // 适配器
        adapter = new LocalImgAdapter(this, data);
        gv.setAdapter(adapter);

        // 获取本地数据，存放到数据源中
        loadImgData();
        // 设置每一个item的点击事件
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String path = data.get(position);
                Intent intent = new Intent();
                intent.putExtra("fPath", path);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        changeStatusBarTextImgColor(true);
    }

    public void changeStatusBarTextImgColor(boolean color) {
        if (color) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        } else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    // 获取本地数据：sd卡中的图片信息
    private void loadImgData() {
        ContentResolver resolver = getContentResolver();
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = resolver.query(uri, null, null, null);
        while (cursor.moveToNext()) {
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            data.add(path);
        }
        adapter.notifyDataSetChanged();
    }
}